//
//  SettingMainViewController.m
//  HTP
//
//  Created by 陈钱钿 on 15/11/3.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "SettingMainViewController.h"
#import "AccountSafeViewController.h"
#import "ChangePwdViewController.h"
#import "AboutAppViewController.h"
#import "Cache_clear.h"
#import <QuartzCore/QuartzCore.h>
#import "PlaceholderTextView.h"
#import "AppDelegate.h"
#import "FeedbackViewController.h"
#import "FeedbackViewModel.h"
#define kBorder 20
#define cellHeight 40
@interface SettingMainViewController () <UITextViewDelegate>
@property (nonatomic, retain) UIView *alterView;
@property (nonatomic, retain) UIView *firstView;
@property (nonatomic, retain) UIView *secondView;
@property (nonatomic, retain) UIView *thirdView;
@property (nonatomic, retain) UIView *fourthView;
@property (nonatomic, retain) UIButton *exitBtn;
@property (nonatomic, retain) UITapGestureRecognizer *accGesture;
@property (nonatomic, retain) UITapGestureRecognizer *FBGesture;
@property (nonatomic, retain) UITapGestureRecognizer *aboutGesture;
@property (nonatomic, retain) UIButton *cache;
@property (nonatomic, strong) UILabel *cacheSize;
@property (nonatomic, strong) PlaceholderTextView *textView;


@property (nonatomic,assign) NSInteger angle;
@end

@implementation SettingMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self computerCache];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.view.backgroundColor = RGBCOLOR(233.0, 233.0, 233.0, 1);
    [self addCustomTitleWithTitle:@"设置"];
    [self addCustomBackBase];
}

//TODO:绘制UI
- (void)initUI {
    _firstView = [self viewWithFrame:CGRectMake(0, kBorder, ScreenWidth, cellHeight) Labeltext:@"帐号与安全" IsAddLine:NO];
    [self btnWithImageName:@"more" andView:_firstView andFrame:CGRectMake(ScreenWidth-30, 9.5, 13, 25)];
    self.accGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(accountForward)];
    [self.firstView addGestureRecognizer:self.accGesture];
    
    _secondView = [self viewWithFrame:CGRectMake(0, CGRectGetMaxY(self.firstView.frame)+kBorder, ScreenWidth, cellHeight+1) Labeltext:@"清理缓存" IsAddLine:YES];
    _cacheSize = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth-145, 0, 100, cellHeight)];
    _cacheSize.textAlignment = _TEXT_ALIGN_RIGHT;
    _cacheSize.textColor = [UIColor grayColor];
    _cacheSize.font = [UIFont systemFontOfSize:15];
    [self.secondView addSubview:self.cacheSize];
    self.cache = [self btnWithImageName:@"huancun" andView:_secondView andFrame:CGRectMake(ScreenWidth-34, 9, 17, 20)];
    [self.cache addTarget:self action:@selector(clearCache_set:) forControlEvents:UIControlEventTouchUpInside];
    
    _thirdView = [self viewWithFrame:CGRectMake(0, CGRectGetMaxY(self.secondView.frame), ScreenWidth, cellHeight+1) Labeltext:@"反馈" IsAddLine:YES];
    [self btnWithImageName:@"more" andView:_thirdView andFrame:CGRectMake(ScreenWidth-30, 9.5, 13, 25)];
    self.FBGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goToFeedback)];
    [self.thirdView addGestureRecognizer:self.FBGesture];

    _fourthView = [self viewWithFrame:CGRectMake(0, CGRectGetMaxY(self.thirdView.frame), ScreenWidth, cellHeight) Labeltext:@"关于青苗" IsAddLine:NO];
    [self btnWithImageName:@"more" andView:_fourthView andFrame:CGRectMake(ScreenWidth-30, 9.5, 13, 25)];
    self.aboutGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(aboutForward)];
    [self.fourthView addGestureRecognizer:self.aboutGesture];
    
    self.exitBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.fourthView.frame)+kBorder, ScreenWidth, cellHeight)];
    [self.exitBtn setBackgroundColor:[UIColor whiteColor]];
    [self.exitBtn setTitle:@"退出当前帐号" forState:UIControlStateNormal];
    [self.exitBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.exitBtn addTarget:self action:@selector(exitClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.exitBtn];
}
//TODO:初始化view的封装
- (UIView *)viewWithFrame:(CGRect)frame Labeltext:(NSString *)text IsAddLine:(BOOL)line {
    UIView *newView = [[UIView alloc] initWithFrame:frame];
    newView.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(kBorder, 0, 120, cellHeight)];
    label.text = text;
    [newView addSubview:label];
    if (line) {
        UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(kBorder, CGRectGetMaxY(label.frame), ScreenWidth-2*kBorder, 1)];
        line.image = [UIImage imageNamed:@"settingline"];
        [newView addSubview:line];
    }
    [self.view addSubview:newView];
    return newView;
}
//TODO:初始化按钮的封装
- (UIButton *)btnWithImageName:(NSString *)ImageName andView:(UIView *)view andFrame:(CGRect)frame{
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    [btn setImage:[UIImage imageNamed:ImageName] forState:UIControlStateNormal];
    [view addSubview:btn];
    return btn;
}

//TODO:退出按钮的点击
- (void)exitClick{
    AppDelegate *tempApp = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [USERCONTENT removeUserBean];
    [tempApp changeLoginVc];
}
/**
 *  yunhe 计算缓存大小
 */
-(void)computerCache{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    DLog(@"paths -----> %@",paths);
    NSString *path = [paths lastObject];
    DLog(@"path --- >%@",path);
    Cache_clear *cache_clear = [[Cache_clear alloc] init];
    _cacheSize.text = [NSString stringWithFormat:@"%.1lfM",[cache_clear folderSizeAtPath:path]];
    DLog(@"%lf",[cache_clear folderSizeAtPath:path]);
}
//TODO: 清理缓存
/**
 *  wangxin 缓存清理
 **/
- (void)clearCache_set:(id)sender {
    [self cachAnimate];
    Cache_clear *cache_clear = [[Cache_clear alloc] init];
    [cache_clear action:sender];
    [self computerCache];
}

//TODO:进入更改密码
- (void)accountForward {
    ChangePwdViewController *changeVc = [[ChangePwdViewController alloc] init];
    [self.navigationController pushViewController:changeVc animated:YES];
}

/*
 cqt 进入关于青苗
 */
- (void)aboutForward {
    AboutAppViewController *aaVc = [[AboutAppViewController alloc] init];
    [self.navigationController pushViewController:aaVc animated:YES];
}

//跳转到反馈界面
- (void)goToFeedback {
    FeedbackViewController *feedbackView = [[FeedbackViewController alloc] init];
    [[NSNotificationCenter defaultCenter] postNotificationName:ROOTGOTONEXTCONTROLLER object:feedbackView];
}

#pragma mark 解决键盘挡住UITextView的方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    NSTimeInterval animationDuration=0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    int offset= -5;
    CGRect rect=CGRectMake(0.0f,offset,ScreenWidth,ScreenHeihgt);
    self.view.frame=rect;
    [UIView commitAnimations];
    return YES;
}

//恢复原始视图位置
-(void)resumeView
{
    NSTimeInterval animationDuration=0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    CGRect rect=CGRectMake(0.0f,64,ScreenWidth,ScreenHeihgt);
    self.view.frame=rect;
    [UIView commitAnimations];
}

//键盘隐藏时调用
-(void)keyboardWillHiden {
    [self resumeView];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //退出键盘
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/**
 *  缓存按钮动画
 */
-(void)cachAnimate{
    CGAffineTransform endAngle = CGAffineTransformMakeRotation(_angle * (M_PI /180.0f));
    [UIView animateWithDuration:0.01 delay:0 options: UIViewAnimationOptionCurveLinear animations:^{
        self.cache.transform = endAngle;
    } completion:^(BOOL finished) {
        _angle += 15;
        if ((_angle > 1000)) {
            _angle = 0;
            [self showSVProgressSuccess:@"清理完毕"];
            return ;
        }
        [self cachAnimate];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
